
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.XJ;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.XJ
{
	/// <summary>
	/// XJMaintenanceDetail service
	/// </summary>
	public partial class XJMaintenanceDetailService : IXJMaintenanceDetailService
	{
		#region Constants

		/// <summary>
		/// Key for caching
		/// </summary>
		private const string XJMAINTENANCEDETAILS_ALL_KEY = "Nop.xjmaintenancedetail.all";
		/// <summary>
		/// Key for caching
		/// </summary>
		/// <remarks>
		/// {0} : xJMaintenanceDetail ID
		/// </remarks>
		private const string XJMAINTENANCEDETAIL_BY_ID_KEY = "Nop.xjmaintenancedetail.id-{0}";
		/// <summary>
		/// Key pattern to clear cache
		/// </summary>
		private const string XJMAINTENANCEDETAILS_PATTERN_KEY = "Nop.xjmaintenancedetail.";
		#endregion

		#region Fields
		private readonly IRepository<XJMaintenanceDetail> _xJMaintenanceDetailRepository;
		private readonly IEventPublisher _eventPublisher;
		private readonly ICacheManager _cacheManager;
		#endregion

		#region Ctor

		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="cacheManager">Cache manager</param>
		/// <param name="xJMaintenanceDetailRepository">XJMaintenanceDetail repository</param>
		/// <param name="eventPublisher">Event published</param>
		public XJMaintenanceDetailService(ICacheManager cacheManager,
			IRepository<XJMaintenanceDetail> xJMaintenanceDetailRepository,
			IEventPublisher eventPublisher)
		{
			this._cacheManager = cacheManager;
			this._xJMaintenanceDetailRepository = xJMaintenanceDetailRepository;
			this._eventPublisher = eventPublisher;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Gets all XJMaintenanceDetails
		/// </summary>
		/// <returns>XJMaintenanceDetails</returns>
		public virtual IList<XJMaintenanceDetail> GetAllXJMaintenanceDetails()
		{
			string key = XJMAINTENANCEDETAILS_ALL_KEY;
			return _cacheManager.Get(key, () =>
			{
				var query = from a in _xJMaintenanceDetailRepository.Table
							orderby a.Id
							select a;
				return query.ToList();
			});
		}

		public virtual IList<XJMaintenanceDetail> GetXJMaintenanceDetails(int maintenanceId)
		{
			var query = from a in _xJMaintenanceDetailRepository.Table
						where a.MaintenanceId == maintenanceId
						orderby a.Id
						select a;
			return query.ToList();
		}

		public virtual IPagedList<XJMaintenanceDetail> GetXJMaintenanceDetails(int maintenanceId, int pageIndex = 0, int pageSize = int.MaxValue)
		{
			var query = from a in _xJMaintenanceDetailRepository.Table
						where a.MaintenanceId == maintenanceId
						orderby a.Id
						select a;
			query = query.OrderBy(c => c.Id);

			//paging
			return new PagedList<XJMaintenanceDetail>(query, pageIndex, pageSize);
		}

		/// <summary>
		/// Gets a XJMaintenanceDetail 
		/// </summary>
		/// <param name="xJMaintenanceDetailId">XJMaintenanceDetail identifier</param>
		/// <returns>XJMaintenanceDetail</returns>
		public virtual XJMaintenanceDetail GetXJMaintenanceDetailById(int xJMaintenanceDetailId)
		{
			if (xJMaintenanceDetailId == 0)
				return null;

			string key = string.Format(XJMAINTENANCEDETAIL_BY_ID_KEY, xJMaintenanceDetailId);
			return _cacheManager.Get(key, () => _xJMaintenanceDetailRepository.GetById(xJMaintenanceDetailId));
		}

		/// <summary>
		/// Inserts a XJMaintenanceDetail
		/// </summary>
		/// <param name="xJMaintenanceDetail">XJMaintenanceDetail</param>
		public virtual void InsertXJMaintenanceDetail(XJMaintenanceDetail xJMaintenanceDetail)
		{
			if (xJMaintenanceDetail == null)
				throw new ArgumentNullException("xJMaintenanceDetail");

			_xJMaintenanceDetailRepository.Insert(xJMaintenanceDetail);

			_cacheManager.RemoveByPattern(XJMAINTENANCEDETAILS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityInserted(xJMaintenanceDetail);
		}

		/// <summary>
		/// Updates the XJMaintenanceDetail
		/// </summary>
		/// <param name="xJMaintenanceDetail">XJMaintenanceDetail</param>
		public virtual void UpdateXJMaintenanceDetail(XJMaintenanceDetail xJMaintenanceDetail)
		{
			if (xJMaintenanceDetail == null)
				throw new ArgumentNullException("xJMaintenanceDetail");

			_xJMaintenanceDetailRepository.Update(xJMaintenanceDetail);

			_cacheManager.RemoveByPattern(XJMAINTENANCEDETAILS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityUpdated(xJMaintenanceDetail);
		}

		/// <summary>
		/// Deletes a XJMaintenanceDetail
		/// </summary>
		/// <param name="xJMaintenanceDetail">XJMaintenanceDetail</param>
		public virtual void DeleteXJMaintenanceDetail(XJMaintenanceDetail xJMaintenanceDetail)
		{
			if (xJMaintenanceDetail == null)
				throw new ArgumentNullException("xJMaintenanceDetail");

			_xJMaintenanceDetailRepository.Delete(xJMaintenanceDetail);

			_cacheManager.RemoveByPattern(XJMAINTENANCEDETAILS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityDeleted(xJMaintenanceDetail);
		}

		#endregion
	}
}
